package com.situ.mall.suanfa;

import io.netty.util.internal.StringUtil;

import java.util.HashMap;
import java.util.Scanner;

/**
 * @ClassName Demo4
 * @Description TODO
 * @Author 周轩霆
 * @Date 2022/9/9 19:31
 **/

public class Demo4 {
    public static void main(String[] args) {
//        int nums[] = {-3, 1, 2, 3, 4};
////        System.out.println(getCount(nums));
//        Scanner scanner = new Scanner(System.in);
//        String nextLine = scanner.nextLine();
//        System.out.println(getMaxLength(nextLine));
        subArraySumsZero();
    }

    public static int getCount(int[] array) {
        int count = 0;
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] + array[i] == 0) {
                    count++;
                    break;
                }
            }
        }
        return count;
    }

    public static int getMaxLength(String str) {
        int max = 0;
        int temp = 1;
        if (str == null) {
            return -1;
        }
        for (int i = 1; i < str.length(); i++) {
            char pre = str.charAt(i - 1);
            char now = str.charAt(i);
            if (now - pre == 1) {
                ++temp;
            } else {
                max = (max > temp ? max : temp);
                temp = 1;
            }
        }
        max = (max > temp ? max : temp);
        return max;
    }
    public static void subArraySumsZero()
    {
        int [] seed = new int[] {1,2,3,4,-9,6,7,-8,1,9};
        int currSum = 0;

        //key: is the currSum  value: the index
        HashMap<Integer, Integer> sumMap = new HashMap<Integer, Integer>();


        for(int i = 0 ; i < seed.length ; i ++)
        {
            currSum += seed[i];
            if(currSum == 0)
            {
                System.out.println("subset : { 0 - " + i + " }");
            }
            else if(sumMap.get(currSum) != null)
            {
                System.out.println("subset : { " + (sumMap.get(currSum) + 1) + " - " + i + " }");
                sumMap.put(currSum, i);
            }
            else
            {
                sumMap.put(currSum, i);
            }
            System.out.println("HASH MAP HAS: " + sumMap);
        }
    }
}
